var screen_width
mui.init();
mui.plusReady(function(){
	//设定状态栏样式
	if (plus.navigator.isImmersedStatusbar() && mui.os.ios) {
		document.getElementById("bar-cover").style.display = 'block';
		document.getElementById("bar-cover").style.backgroundImage = "";
	}
	if (plus.navigator.isImmersedStatusbar() && mui.os.android) {
		document.getElementById("bar-cover").style.display = 'block';
		document.getElementById("bar-cover").style.backgroundColor = "";
	}
	screen_width = plus.screen.resolutionWidth;
});

var appid = 'kou72um0ioak1kubrz0klwx8a7s3l261mgt54vbyotnblnby';
var appkey = 'o56bexx1vr522x7ygfkvi7efvbhhmo6x2oaf3txb75wi2509';
AV.initialize(appid, appkey);
var Channel = AV.Object.extend('Channel');
var channel_query = new AV.Query(Channel);
channel_query.ascending('index');
channel_query.include('topics');
channel_query.find().then(function(eles){
	var channels = [];
	var relation = eles[0].relation('topics');
	relation.query().find().then(function(topics){
		var topic_ele = topics[0];
		var top_topic = {
			'id': topic_ele.id,
			'name': topic_ele.get('name'),
			'description': topic_ele.get('description'),
			'cover': topic_ele.get('coverBanner'),
		};
		var top_html = template('top-tpl', {
			'topic': top_topic,
		});
		document.getElementById("top-topic").innerHTML = top_html;
	}, function(error){
		console.log(error.message);
	});

	var channels = {};
	for(var i = 1; i < eles.length; i++){
		(function(i){
			var channel = {
				'id': eles[i].id,
				'name': eles[i].get('name'),
				'description': eles[i].get('description'),
				'index': eles[i].get('index'),
				'width': screen_width
			};
			var topics = [];
			var relation = eles[i].relation('topics');
			relation.query().find().then(function(topic_eles){
				for(var j = 0; j < (topic_eles.length<4?topic_eles.length:4); j++){
					var ele = topic_eles[j];
					var topic = {
						'id': ele.id,
						'name': ele.get('name'),
						'description': ele.get('description'),
						'rec_count': ele.get('recCounter'),
						'cover': ele.get('coverStandard'),
					};
					var source_count = 0;
					var sources = JSON.parse(ele.get('sourceCounter'));
					for(var s in sources){
						if(sources[s] != null){
							source_count += sources[s];
						}
					}
					topic['source_count'] = source_count;
					topics.push(topic);
				}
				channel['topics'] = topics;
				channel['length'] = topic_eles.length;
				channel_html = template('channel-tpl', channel);
				mui('#channels .channel')[channel.index-1].innerHTML = channel_html;
			}, function(error){
				console.log(error.message);
			});
		})(i);
	}
}, function(error){
	console.log(error.message);
});


mui('body').on('tap', '.topic', function(){
	var fid = this.getAttribute('data-id');
	mui.openWindow({
		id: 'topic',
		url: 'topic.html',
		styles: {
			top: 0,
			bottom: 0,
			bounce: 'vertical',
		},
		extras: {
			topic_id: fid,
		},
		show: {
	    		autoShow: true,
	    		aniShow: 'pop-in',
	    		duration: 300,
	   },
	   waiting: {
	   		autoShow: false,
	   }
	});
});